package com.atguigu.controller;

import com.atguigu.client.CartFeignClient;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.entity.SkuInfo;
import com.atguigu.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class WebCartController {

    @Autowired
    private CartFeignClient cartFeignClient;
    @Autowired
    private ProductFeignClient productFeignClient;

    //http://cart.gmall.com/addCart.html?skuId=24&skuNum=1
    @GetMapping("addCart.html")
    public String login(@RequestParam Long skuId, @RequestParam Long skuNum, HttpServletRequest request) {
//        String userTempId = AuthContextHolder.getUserTempId(request);
        cartFeignClient.addToCart(skuId, skuNum);
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        request.setAttribute("skuInfo", skuInfo);
        request.setAttribute("skuNum", skuNum);
        return "cart/addCart";
    }

    @GetMapping("cart.html")
    public String cartHtml() {
        return "cart/index";
    }
}
